// ignore_for_file: prefer_final_fields

import 'package:favourcate/core/model/meal_model.dart';
import 'package:favourcate/core/viewmodel/filter_view_model.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';

class GLFavorViewModel extends ChangeNotifier {
  List<GLMealModel> _favorMeals = [];
  late GLFilterViewModel _filterStore;

  List<GLMealModel> get favorMeals {
    return _favorMeals.where((meal) {
      // 过滤
      if (_filterStore.isGlutenFree && !meal.isGlutenFree) return false;
      if (_filterStore.isLactoseFree && !meal.isLactoseFree) return false; 
      if (_filterStore.isVegetarial && !meal.isVegetarian) return false; 
      if (_filterStore.isVegan && !meal.isVegan) return false; 
      return true;
    }).toList();
  }

  void updateFilters(GLFilterViewModel filterStore) {
    _filterStore = filterStore;
  }

  void addMeal (meal) {
    _favorMeals.add(meal);
    notifyListeners();
  }

  void removeMeal(meal) {
    _favorMeals.remove(meal);
    notifyListeners();
  }

  bool isFavor(meal) {
    return _favorMeals.contains(meal);
  }

  void handleMeal(meal) {
    if (!isFavor(meal)) {
      addMeal(meal);
    }else {
      removeMeal(meal);
    }
    
  }

}